HowtoBecomeaProgrammer
About UsContact UsString formatting involves strings, which have been discussed previously, and data to insert into the string.
There are two ways of accomplishing this, the first is the .format(). The basic syntax is:
"{}".format(variable)
You can of course put in string you would like in the quotations, however the curly brackets are where the variable would be placed in the string. You can have multiple sets of curly brackets as well. The syntax for that would look like this:
"{} {} {}".format(variable1, variable2, variable3)
One more thing to note for now is that you can put in a data type in the parenthesises, you aren't limited to only putting variables in the parenthesises. I challenge you to use the .format() method to print out the following phrase: 'Hello, my full name is (your full name)! Nice to meet you!' You can either put the string for you name in the .format() directly, or set your full name equal to variables and then using the .format().
The second type of string formatting is the % sign. The syntax for this is relatively the same. Here's an example:
'The first 10 letters of the alphabet are: %s' % 'abcdefghij'
Notice the %s next to the %, as the data type that replaced the % was a string, we had to compensate for that by placing %s, and the same rule applies for integers, %d. For lists, you can use the %s. You can have multiple %'s. I once again challenge you to do the same thing as you did with the .format() method, printing 'Hello, my full name is (your full name)! Nice to meet you!'
Once you have finished the challenge move on to the next lesson!